﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cadcam7Moddel.Model;
using Cadcam7Moddel.Context;
using camcad7Home.CultureHelper;
namespace camcad7Home.Controllers
{
    public class LienheController : CultureController
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /Lienhe/
       public ActionResult jsonList()
        {


            var list = from n in db.Lienhe
                       orderby n.ID descending
                       select new {n.ID,n.Ten,n.Sodt,n.Email,n.Tieude,n.Ngaydk };
			
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Lienhe/Details/5

        public ActionResult Details(int id = 0)
        {
            Lienhe lienhe = db.Lienhe.Find(id);
            if (lienhe == null)
            {
                return HttpNotFound();
            }
            return View(lienhe);
        }

        //
        // GET: /Lienhe/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Lienhe/Create

        [HttpPost]
        public ActionResult Create(Lienhe lienhe)
        {
            if (ModelState.IsValid)
            {
                db.Lienhe.Add(lienhe);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(lienhe);
        }

        //
        // GET: /Lienhe/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Lienhe lienhe = db.Lienhe.Find(id);
            if (lienhe == null)
            {
                return HttpNotFound();
            }
            return View(lienhe);
        }

        //
        // POST: /Lienhe/Edit/5

        [HttpPost]
        public ActionResult Edit(Lienhe lienhe)
        {
            if (ModelState.IsValid)
            {
                db.Entry(lienhe).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(lienhe);
        }

        //
        // GET: /Lienhe/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Lienhe lienhe = db.Lienhe.Find(id);
            if (lienhe == null)
            {
                return HttpNotFound();
            }
            return View(lienhe);
        }

        //
        // POST: /Lienhe/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Lienhe lienhe = db.Lienhe.Find(id);
            db.Lienhe.Remove(lienhe);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        public ActionResult ajDelete(int id)
        {
            Lienhe cauhinh = db.Lienhe.Where(m => m.ID == id).FirstOrDefault();
            if (cauhinh != null)
            {
                db.Lienhe.Remove(cauhinh);
                //db.SaveChanges();
                db.SaveChanges();
                return Json(new { success = true });
            }
            else
            {
                return Json(new { success = false });
            }
        }
    }
}